import { findIntersection } from '@/utils';

// 给路由增加 key
export const mapMenuAddKey = (menus=[], parentKey='') => {
    return (menus || []).map((item, index) => {
        let {children, ...o} = item;

        if(typeof o.url === 'string') {
            o.key = o.url;
        }
        if(typeof o.key !== 'string' && o.url ) {
            o.key = `${parentKey ? parentKey+'/' : ''}${o.url || index}`.replace(/(\/\/)/g, '/');
        }

        if(Array.isArray(children) && children.length) {
            o.children = mapMenuAddKey(children, o.key);
        }

        return o;
    });
};

/*
    获取有权限的菜单
    data: 菜单数据
    modules: 权限数据
    return: 有权限的菜单数据
*/
export const getAuthMenu = (data=[], modules=[]) => {
    return data.map(item => {
        let {component, layout, showInMenu, ...o} = item,
            auth = (o.auth && typeof o.auth === 'string') ? [o.auth] : (Array.isArray(o.auth) && o.auth.length ? o.auth : false);

        if(showInMenu === false) return undefined;

        if(auth) {
            if(findIntersection(modules, auth).length) {
                if(Array.isArray(o.children) && o.children.length) {
                    let res = getAuthMenu(o.children, modules);

                    if(res.length) {
                        o.children = res;
                    }else{
                        delete o.children;
                    }
                }

                return o;
            }
            return undefined;
        }else{
            if(Array.isArray(o.children) && o.children.length) {
                let res = getAuthMenu(o.children, modules);

                if(res.length) {
                    o.children = res;
                }

                return undefined;
            }else{
                return o;
            }
        }
        if(auth && findIntersection(modules, auth).length) {
            if(Array.isArray(o.children) && o.children.length) {
                let res = getAuthMenu(o.children, modules);

                if(res.length) {
                    o.children = res;
                }else{
                    delete o.children;
                }
            }

            return o;
        }else if(!auth && Array.isArray(o.children) && o.children.length) {
            let res = getAuthMenu(o.children, modules);

            if(res.length) {
                o.children = res;
            }

            return undefined;
        }

        return undefined;
    }).filter(Boolean);
};


/**
 *打平路由
 *
 * @param {*} [data=[]] 路由tree数据
 * @return {*} 打平后的路由
 */
export const flatRoutes = (data=[]) => {
    let routers = [];
    (data || []).map(item => {
        if(item.key || item.url) {
            if(item.url) routers.push(item); // 只要有url就先记录路由

            if(Array.isArray(item.children) && item.children.length) {
                let r = flatRoutes(item.children);

                routers = routers.concat(r);
            }
        }
    });

    return routers;
};

/*
    获取当前页面的 路由路径
*/
export const getPagePath = (pathname='', routers=[]) => {
    let result = [];
    for(let i=0;i<routers.length;i++) {
        let cur = routers[i];

        if(cur.key === pathname) result.push(cur);

        if(Array.isArray(cur.children) && cur.children.length) {
            let r = getPagePath(pathname, cur.children);
            result = r.concat(result);
            if(r.length) {
                result.push(cur);
            }
        }
    }

    return result;
};